home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ian & Stuart's Australian Mac 1993 September
/
September 93.iso
/
Archives
/
Utilities
/
Text
/
wordcount
/
WordCount.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-11
|
3KB
|
151 lines
/*
* WordCount.c
*
* BBEdit extension by
* Kamal Abdali, P.O. Box 65207, Washington, DC 20035.
* December 1992
*
* Displays the number of sentences, paragrphs, nonempty lines,
* words, and characters in the selection.
* If the selection is null, the info is for the whole file.
*
* To create this extension in THINK C, include this file and MacTraps
* in the project, build as a code resource with WordCount.Rsrc,
* and put the resource in the BBEdit extension folder.
*
*/
#include <Packages.h> /* For NumToString */
#include <SetupA4.h>
#include "ExternalInterface.h"
/* Dialog ID & DITL item numbers */
#define kDlg 128
enum {iOK = 1, iSeparator = 3, iPara, iSentence, iLine, iWord, iChar};
long paraCount = 0;
long sentenceCount = 0;
long lineCount = 0;
long wordCount = 0;
long charCount = 0;
#define WordEnd(c) ((c == ' ') || (c == '\t'))
#define LineEnd(c) ((c == '\n') || (c == '\r'))
#define SentenceEnd(c) ((c == '.') || (c == '!') || (c == '?'))
static long get_handle_size(Handle h)
{
asm {
movea.l h, a0
_GetHandleSize
}
}
static void SetCountStr(DialogPtr dPtr, short itemNo, long count)
{
Handle itemHdl;
short type;
Rect rect;
unsigned char str[16];
GetDItem(dPtr, itemNo, &type, &itemHdl, &rect);
NumToString(count, str);
SetIText(itemHdl, str);
}
static void WordCount(ExternalCallbackBlock *callbacks, WindowPtr w)
{
Handle text;
Size size;
long selStart, selEnd, firstChar, i;
Boolean paraEndFound, sentenceEndFound, lineEndFound, wordEndFound;
char ch, prevCh;
text = callbacks->GetWindowContents(w);
size = get_handle_size(text);
callbacks->GetSelection(&selStart, &selEnd, &firstChar);
if (selStart == selEnd) {
selStart = 0;
selEnd = size;
}
wordEndFound = lineEndFound = true;
sentenceEndFound = paraEndFound = false;
ch = ' ';
for (i = selStart; i < selEnd; i++) {
prevCh = ch;
ch = (*text)[i];
if (WordEnd(ch)) {
wordEndFound = true;
} else if (LineEnd(ch)) {
lineEndFound = wordEndFound = true;
if (LineEnd(prevCh))
paraEndFound =true;
} else if (SentenceEnd(ch)) {
sentenceEndFound = true;
} else {
if (wordEndFound) {
wordEndFound = false;
wordCount++;
}
if (lineEndFound) {
lineEndFound = false;
lineCount++;
}
if (sentenceEndFound) {
sentenceEndFound = false;
if (!SentenceEnd(prevCh))
sentenceCount++;
}
if (paraEndFound) {
paraEndFound = false;
paraCount++;
}
}
}
if (sentenceEndFound)
sentenceCount++;
paraCount++;
charCount = selEnd - selStart;
}
static void Report(ExternalCallbackBlock *callbacks)
{
DialogPtr dPtr;
GrafPtr savePort;
short item;
GetPort(&savePort);
dPtr = callbacks->CenterDialog(kDlg);
SetPort(dPtr);
callbacks->FrameDialogItem(dPtr, iSeparator);
SetCountStr(dPtr, iPara, paraCount);
SetCountStr(dPtr, iSentence, sentenceCount);
SetCountStr(dPtr, iLine, lineCount);
SetCountStr(dPtr, iWord, wordCount);
SetCountStr(dPtr, iChar, charCount);
do
ModalDialog(callbacks->StandardFilter, &item);
while (item != iOK);
DisposDialog(dPtr);
SetPort(savePort);
}
pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
{
RememberA0();
SetUpA4();
if (w && callbacks->version >= 2) {
WordCount(callbacks, w);
Report(callbacks);
}
RestoreA4();
}